package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.EquipmentDao;
import cn.topevery.garden.entity.po.Equipment;
import cn.topevery.garden.entity.query.EquipmentQuery;
import cn.topevery.garden.entity.vo.EquipmentVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-02-23
 */
@Repository
public class EquipmentDaoImpl extends BaseDaoImpl<Equipment> implements EquipmentDao {
    @Override
    public <P extends DbCsp> List<Equipment> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<EquipmentVO> listPage(EquipmentQuery query) {
        String table = "t_equipment te";

        String columns = "te.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("te.c_id desc");
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "te.c_code", ScriptConditionEnum.Eq, query.getCode())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EquipmentVO.class);
    }

    @Override
    public Equipment getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void checkList(List<Integer> idList, Integer checkStatus) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_equipment te");
        sqlUpdateMate.setColumnValue(true, "te.c_check_status", checkStatus)
                .where(true, "te.c_id", ScriptConditionEnum.In, idList);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public DataPager<EquipmentVO> getEquipmentListByPointId(EquipmentQuery query) {
        String table = "t_equipment te LEFT JOIN t_sys_user tsu on te.c_checker_id=tsu.c_id left join t_point t on t.c_point_id=te.c_point_id";
        String columns = "te.*,tsu.c_user_name checker, t.c_point_num pointNum";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("te.c_id desc");
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getPointId()), "te.c_point_id", ScriptConditionEnum.Eq, query.getPointId())
                .where(query.getCheckStatus() != null, "te.c_check_status", ScriptConditionEnum.Eq, query.getCheckStatus())
                .where(query.getCallStatus() != null, "te.c_call_status", ScriptConditionEnum.Eq, query.getCallStatus())
                .where(StringUtils.isNotBlank(query.getName()), "te.c_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotBlank(query.getCode()), "te.c_code", ScriptConditionEnum.Like, query.getCode());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EquipmentVO.class);
    }

    @Override
    public void setEquipmentStatus(List<Integer> idList, Integer callStatus) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_equipment te");
        sqlUpdateMate.setColumnValue(true, "te.c_call_status", callStatus)
                .where(true, "te.c_id", ScriptConditionEnum.In, idList);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
